package com.epro.standard.frontend.tagext;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class ViewTableTag extends TagSupport {
    private String sortBy = "";
    private String sortCol = "";
    private boolean asc = true;
    private int Page = 1;
    private int index = 0;
    private int indexSort = -1;
    //

    private String[] alignValue = null;
    private String[] maxDataLength = null;
    private int maxRecord = 11;
    private String[][] data = null;
    private String[] head = null;
    private String hintID = "hint1";
    private String tableWidth = "";

    private String noDetail = "true";
    private String noEdit = "false";
    private String editURL = "";
    private String editWindowWidth = "30";
    private String editWindowHeight = "20";
    private String detailURL = "";
    private String detailWindowWidth = "40";
    private String detailWindowHeight = "30";
    private String[] dataWidth = null;
    private String isSort = "true";

    public void setIsSort(String isSort) {
        this.isSort = isSort;
    }

    public void setDataWidth(String[] dataWidth) {
        this.dataWidth = dataWidth;
    }

    public void setMaxRecord(int maxRecord) {
        this.maxRecord = maxRecord;
    }

    public void setAlignValue(String[] alignValue) {
        this.alignValue = alignValue;
    }

    public void setMaxDataLength(String[] maxDataLength) {
        this.maxDataLength = maxDataLength;
    }

    public void setData(String[][] data) {
        this.data = data;
    }

    public void setHead(String[] head) {
        this.head = head;
    }

    public void setHintID(String hintID) {
        this.hintID = hintID;
    }

    public void setTableWidth(String tableWidth) {
        this.tableWidth = tableWidth;
    }

    public void setNoDetail(String noDetail) {
        this.noDetail = noDetail;
    }

    public void setNoEdit(String noEdit) {
        this.noEdit = noEdit;
    }

    public void setEditURL(String editURL) {
        String str = "";

        java.util.StringTokenizer token = 
            new java.util.StringTokenizer(editURL, "?");

        if (token.countTokens() >= 1)
            str += editURL + "&";
        else
            str += editURL + "?";

        this.editURL = str;
    }

    public void setEditWindowWidth(String editWindowWidth) {
        this.editWindowWidth = editWindowWidth;
    }

    public void setEditWindowHeight(String editWindowHeight) {
        this.editWindowHeight = editWindowHeight;
    }

    public void setDetailURL(String detailURL) {
        String str = "";

        java.util.StringTokenizer token = 
            new java.util.StringTokenizer(detailURL, "?");

        if (token.countTokens() >= 1)
            str += detailURL + "&";
        else
            str += detailURL + "?";

        this.detailURL = str;
    }

    public void setDetailWindowWidth(String detailWindowWidth) {
        this.detailWindowWidth = detailWindowWidth;
    }

    public void setDetailWindowHeight(String detailWindowHeight) {
        this.detailWindowHeight = detailWindowHeight;
    }

    private String getDetailURL() {
        String str = "";

        java.util.StringTokenizer token = 
            new java.util.StringTokenizer(detailURL, "?");

        if (token.countTokens() > 1)
            str += detailURL + "&index=";
        else
            str += detailURL + "?index=";

        return str;
    }

    public int doStartTag() throws JspException {
        init();
        try {
            String str = "";
            JspWriter out = pageContext.getOut();

            str += "<FORM name=\"master1\" method=\"POST\" >\n";

            str += getHeadSort();
            str += getBlockData();

            out.println(str);
        } catch (java.io.IOException ioe) {
            ioe.printStackTrace();
        }

        return EVAL_BODY_INCLUDE;
    }

    public int doEndTag() throws JspException {
        init();
        try {
            String str = "";
            JspWriter out = pageContext.getOut();
            javax.servlet.http.HttpServletRequest request = 
                (javax.servlet.http.HttpServletRequest)pageContext.getRequest();

            //			str += "<INPUT TYPE=\"hidden\" name =\"redirectPage\" value=\""+request.getRequestURI()+"\">";
            str += "</FORM>";

            out.println(str);
        } catch (java.io.IOException ioe) {
            ioe.printStackTrace();
        }

        return EVAL_PAGE;
    }

    private void init() {
        javax.servlet.http.HttpServletRequest request = 
            (javax.servlet.http.HttpServletRequest)pageContext.getRequest();

        String sortBy = request.getParameter("sortBy");
        String sortCol = head[0];

        if (sortBy != null) {
            for (int i = 0; i < head.length; i++) {
                if (Integer.parseInt(sortBy) == i) {
                    sortCol = head[i];
                    indexSort = i;
                    break;
                }
            }
        }
        boolean asc = true;
        if (request.getParameter("isAsc") != null) {
            asc = 
Boolean.valueOf(request.getParameter("isAsc")).booleanValue();
        }
        //********************** calculate number of page ************************
        if (request.getParameter("page") != null)
            Page = Integer.parseInt(request.getParameter("page"));

        // *****************************************************************************

        // ********************** calculate  index of record ************************
        if (request.getParameter("page") != null) {
            index = (Page - 1) * maxRecord;
        }
        // *****************************************************************************

        this.sortBy = sortBy;
        this.sortCol = sortCol;
        this.asc = asc;
    }

    private String getHeadSort() {
        String str = "";

        str += "<epro:viewtable ";

        str += " currentSort='" + sortBy + "'";
        str += " idForm=\"master1\"  ";
        str += " noDetail='" + noDetail.toLowerCase() + "' ";
        str += " noEdit='" + noEdit.toLowerCase() + "' ";
        if (!(tableWidth.equals("")))
            str += " tableWidth=\"" + tableWidth + "\"";

        if (this.isSort != null)
            str += " isSort=\"" + isSort + "\"";
        str += ">\n";
        for (int i = 0; i < head.length; i++) {
            str += "   <blockSort label='" + head[i] + "'";
            str += 
((sortBy != null) && (Integer.parseInt(sortBy) == i)) ? " sort=\"true\" " : 
" sort=\"false\"";
            if (i == indexSort)
                str += " asc='" + asc + "'";
            if (dataWidth != null)
                str += " width=\"" + dataWidth[i] + "\"";
            str += " />\n";
        }
        str += "</epro:viewtable>\n";
        return str;
    }
    /*	
	private void checkMaxDataLength()
	{
		String[] newMax = new String[head.length];
		
		if (maxDataLength.length != head.length)
		{
			for(int i=0;i<head.length;i++)
			{
				if (i<maxDataLength.length)
					newMax[i] = maxDataLength[i];
				else
					newMax[i] = "0";				
			}
			maxDataLength = newMax;
		}
	}
*/

    private String getBlockData() {
        String str = "";

        for (int i = index; i < (index + maxRecord); i++) {
            if (i >= data[0].length) {
                str += "<epro:viewtable ";
                if (!(tableWidth.equals("")))
                    str += " tableWidth=\"" + tableWidth + "\"";
                str += ">\n";
                for (int j = 0; j < head.length; j++)
                    str += "   <blockEmpty/>\n";
                str += "</epro:viewtable>\n";
            } else {
                str += 
"<epro:viewtable rowID='" + (i - index) + "' idForm=\"master1\" hintID=\"" + 
 hintID + "\"  editURL=\"" + editURL + "?index=" + i + "\" editWindowWidth='" + 
 editWindowWidth + "' editWindowHeight='" + editWindowHeight + "' ";
                if (noDetail.toLowerCase().equals("false"))
                    str += 
" detailURL=\"" + getDetailURL() + i + "\" detailWindowWidth='" + 
 detailWindowWidth + "' detailWindowHeight='" + detailWindowHeight + "' ";
                str += " noDetail='" + noDetail.toLowerCase() + "' ";

                str += " noEdit='" + noEdit.toLowerCase() + "' ";
                if (!(tableWidth.equals("")))
                    str += " tableWidth=\"" + tableWidth + "\"";

                str += ">\n";

                for (int j = 0; j < head.length; j++) {
                    str += "<blockData label=\"" + data[j][i] + "\" ";
                    if (!(alignValue[j].equals("")))
                        str += " alignValue='" + alignValue[j] + "' ";
                    if (!(maxDataLength[j].equals("0")))
                        str += "maxLength='" + maxDataLength[j] + "' ";
                    if (dataWidth != null)
                        str += " width=\"" + dataWidth[j] + "\"";
                    str += "/>\n";
                }
                str += "</epro:viewtable>\n";
            }
        }
        return str;
    }
}

